package com.fosss.band.service.impl;

import com.fosss.band.entity.*;
import com.fosss.band.entity.vo.ConcertVo;
import com.fosss.band.mapper.BandMapper;
import com.fosss.band.mapper.ConcertMapper;
import com.fosss.band.mapper.FanConcertMapper;
import com.fosss.band.service.ConcertService;
import com.fosss.band.service.FanService;
import com.fosss.band.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.fosss.band.utils.ResultCodeEnum.MESSAGE_UN_ENOUGH;

/**
 * @author: fosss
 * Date: 2023/12/27
 * Time: 20:39
 * Description:
 */
@Service
public class ConcertServiceImpl implements ConcertService {

    @Resource
    private ConcertMapper concertMapper;
    @Resource
    private FanService fanService;
    @Resource
    private FanConcertMapper fanConcertMapper;
    @Resource
    private BandMapper bandMapper;

    /**
     * 分页条件查询
     */
    @Override
    public Result getPageByCondition(Integer userId, Integer page, Integer size, String keyword) {
        PageHelper.startPage(page, size);
        List<Concert> list = concertMapper.getByCondition(keyword);
        List<ConcertVo> collect = list.stream().map(concert -> {
            ConcertVo concertVo = new ConcertVo();
            BeanUtils.copyProperties(concert, concertVo);
            //设置乐队名
            Band band = bandMapper.selectById(concertVo.getBandId());
            concertVo.setBandName(band.getName());
            //查询用户是否报名
            FanConcert fanConcert = fanConcertMapper.select(userId, concertVo.getId());
            concertVo.setAttended(fanConcert != null);
            return concertVo;
        }).collect(Collectors.toList());

        PageInfo<ConcertVo> pageInfo = new PageInfo<>(collect);
        return Result.ok().data("rows", pageInfo.getList()).data("total", pageInfo.getTotal());
    }

    /**
     * 添加演唱会
     * createTime得前端传过来
     */
    @Override
    public Result add(Concert concert) {
        if (StringUtils.isEmpty(concert.getName()) || StringUtils.isEmpty(concert.getPlace()) || concert.getCreateTime() == null
                || concert.getBandId() == null) {
            return Result.error().code(MESSAGE_UN_ENOUGH.getCode()).message(MESSAGE_UN_ENOUGH.getMessage());
        }
        concertMapper.insert(concert);
        return Result.ok();
    }

    /**
     * 根据演唱会id查询演唱会
     */
    @Override
    public Result getById(Integer id) {
        Concert concert = concertMapper.selectById(id);
        return Result.ok().data("row", concert);
    }

    /**
     * 修改演唱会
     */
    @Override
    public Result update(Concert concert) {
        if (StringUtils.isEmpty(concert.getName()) || StringUtils.isEmpty(concert.getPlace()) || concert.getCreateTime() == null
                || concert.getBandId() == null) {
            return Result.error().code(MESSAGE_UN_ENOUGH.getCode()).message(MESSAGE_UN_ENOUGH.getMessage());
        }
        concertMapper.update(concert);
        return Result.ok();
    }

    /**
     * 删除演唱会
     */
    @Override
    public Result delete(Integer id) {
        concertMapper.deleteById(id);
        return Result.ok();
    }

    /**
     * 根据演唱会id获取参加的歌迷信息
     */
    @Override
    public Result getFansByConcertId(Integer id) {
        List<User> list = fanService.getFansByConcertId(id);
        return Result.ok().data("rows", list);
    }

    /**
     * 参加或退出演唱会
     */
    @Override
    public Result updateAttendStatus(Integer fanId, Integer concertId) {
        //查询是否参加了演唱会，来判断此次操作是参加还是退出
        FanConcert fanConcert = fanConcertMapper.select(fanId, concertId);
        if (fanConcert == null) {
            //以前未参加，执行添加操作
            FanConcert fc = new FanConcert();
            fc.setFanId(fanId);
            fc.setConcertId(concertId);
            fanConcertMapper.insert(fc);
        } else {
            //退出演唱会，执行删除操作
            fanConcertMapper.delete(fanId, concertId);
        }
        return Result.ok();
    }

    /**
     * 查询用户参加的演唱会
     */
    @Override
    public Result getAttendedConcerts(Integer userId) {
        //查询该用户参加的演唱会id
        List<Integer> attendedIds = fanConcertMapper.selectConcertIdsByFanId(userId);
        //批量查询演唱会信息
        List<ConcertVo> collect = new ArrayList<>();
        if (attendedIds != null && attendedIds.size() > 0) {
            List<Concert> list = concertMapper.selectByIds(attendedIds);
            //封装数据
            collect = list.stream().map(concert -> {
                ConcertVo concertVo = new ConcertVo();
                BeanUtils.copyProperties(concert, concertVo);
                //设置乐队名
                Band band = bandMapper.selectById(concertVo.getBandId());
                concertVo.setBandName(band.getName());
                return concertVo;
            }).collect(Collectors.toList());
        }


        return Result.ok().data("rows", collect);
    }
}
